package com.demo.common.core.exception;


import com.demo.common.core.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.util.Objects;


@SuppressWarnings("all")
@RestControllerAdvice
@Slf4j
public class BaseExceptionHandler {

    private void print(Throwable e){
        e.printStackTrace();
    }

    /**
     * BaseException 异常
     * @param e BaseException
     * @return DataResult
     */
    @ExceptionHandler(BaseException.class)
    public Response BaseException(BaseException e) {
        print(e);
        return Response.fail(e.getResponseErrorCode());
    }

    /**
     * MaxUploadSizeExceededException 异常
     * @param e MaxUploadSizeExceededException
     * @return DataResult
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Response BaseException(MaxUploadSizeExceededException e) {
        print(e);
        return Response.fail(ResponseErrorCode.PAYLOAD_TOO_LARGE);
    }


    /**
     * BindException 异常
     * @param e BindException
     * @return DataResult
     */
    @ExceptionHandler({BindException.class})
    public Response handleBindException(BindException e) {
        print(e);
        FieldError fieldError = e.getFieldError();
        if(Objects.isNull(fieldError)){
            return Response.fail(ResponseErrorCode.BAD_REQUEST);
        }
        return Response.fail(ResponseErrorCode.BAD_REQUEST, fieldError.getDefaultMessage());
    }

    /**
     * MethodArgumentNotValidException 异常
     * @param e MethodArgumentNotValidException
     * @return DataResult
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public Response handleBindException(MethodArgumentNotValidException e) {
        print(e);
        BindingResult bindingResult = e.getBindingResult();
        if(Objects.isNull(bindingResult)){
            return Response.fail(ResponseErrorCode.BAD_REQUEST);
        }
        FieldError fieldError = bindingResult.getFieldError();
        if(Objects.isNull(fieldError)){
            return Response.fail(ResponseErrorCode.BAD_REQUEST);
        }
        return Response.fail(ResponseErrorCode.BAD_REQUEST, fieldError.getDefaultMessage());
    }



    /**
     * HttpMediaTypeNotSupportedException 异常
     * @param e HttpMediaTypeNotSupportedException
     * @return DataResult
     */
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public Response handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        print(e);
        return Response.fail(ResponseErrorCode.BAD_REQUEST, "Data type error");
    }


    /**
     * HttpMessageNotReadableException 异常
     * @param e HttpMessageNotReadableException
     * @return DataResult
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Response HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        print(e);
        return Response.fail(ResponseErrorCode.BAD_REQUEST);
    }


    /**
     * HttpRequestMethodNotSupportedException 异常
     * @param e HttpRequestMethodNotSupportedException
     * @return DataResult
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Response HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        print(e);
        return Response.fail(ResponseErrorCode.BAD_REQUEST, "Request method error");
    }

    /**
     * NoHandlerFoundException 异常
     * @param e NoHandlerFoundException
     * @return DataResult
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public Response NoHandlerFoundException(NoHandlerFoundException e) {
        print(e);
        return Response.fail(ResponseErrorCode.NOT_FOUND);
    }

    /**
     * Exception 异常
     * @param e Exception
     * @return DataResult
     */
    @ExceptionHandler(Exception.class)
    public Response Exception(Exception e) {
        print(e);
        return Response.fail(ResponseErrorCode.INTERNAL_SERVER_ERROR);
    }
}

